<?php

/*
 * outhor wjg
* 通话记录
*/

class CalloutController extends Controller_Base{

    public function indexAction(){

        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $mediationOrganId = trim($request->getQuery('mediationOrganId', 0));
        $mediatorId = $request->getQuery('mediatorId', '');
        $callPhone = $request->getQuery('callPhone', '');
        $caseId = $request->getQuery('caseId', '');
        $talkType = $request->getQuery('talkType', '');
        $importRecordId = $request->getQuery('importRecordId', 0);
        $assetsId = $request->getQuery('assetsId', 0);
        $starttalkTime = $request->getQuery('starttalkTime', 0);
        $endtalkTime = $request->getQuery('endtalkTime', 0);
        $contractNo = $request->getQuery('contractNo', '');

        $litigantName = $request->getQuery('litigantName', '');
        $is_show = intval($request->getQuery('is_show', 0));
        $limit = intval($request->getQuery('limit', 10));
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;

        $query = array('limit' => $limit);
        $query['mediationOrganId'] = $mediationOrganId;
        $query['mediatorId'] = $mediatorId;
        $query['callPhone'] = $callPhone;
        $query['caseId'] = $caseId;
        $query['litigantName'] = $litigantName;
        $query['limit'] = $limit;
        $query['talkType'] = $talkType;
        $query['importRecordId'] = $importRecordId;
        $query['assetsId'] = $assetsId;
        $query['starttalkTime'] = $starttalkTime;
        $query['endtalkTime'] = $endtalkTime;
        $query['contractNo'] = $contractNo;


        $where = $this->getWhere();
        array_push($where,"callType = 1");

        if($mediatorId){//调解员
            array_push($where,"mediatorId = '{$mediatorId}' ");
        }
        if($callPhone){//通话号码
            array_push($where,"callPhone = '{$callPhone}'");
        }
        if($caseId){//按照案件号查询
            array_push($where,"caseId = '{$caseId}'");
        }
        if($talkType){//按照案件号查询
            array_push($where,"talkType = '{$talkType}'");
        }
        if($litigantName){
            array_push($where,"callName like '%{$litigantName}%'");
        }
        if($importRecordId){
            array_push($where,"importRecordId = '{$importRecordId}'");
        }
        if($assetsId){
            array_push($where,"assetsId = '{$assetsId}'");
        }
        if($starttalkTime){
            array_push($where,"talkTime >= '{$starttalkTime}'");
        }
        if($endtalkTime){
            array_push($where,"talkTime <= '{$endtalkTime}'");
        }
        if($contractNo){
            array_push($where,"contractNo = '{$contractNo}'");
        }


        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if($add_begin_end){
            $query['add_begin_end'] = $add_begin_end;
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "callTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "callTime >= {$begin_date}");
            }
        }

        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => 'id DESC'
        );
        $record = Module_Mediate_CallRecord::getInstance()->getAllBySearch(array("*"),$seacher,true);

        $total = Module_Mediate_CallRecord::getInstance()->getAllBySearch(['count(*) as total'],array('where' => $where),true,true);

        $total = $total[0]['total'];

        $totalPage = ceil($total / $limit);
        if(!empty($record)){
            foreach($record as $k=>$v){
                $record[$k]['talkTime'] = Lib_Function::getInstance()->Sec2Time($v['talkTime']);
            }
        }

        //赋值调解部门
        $this ->getMediationOrgan();
        //赋值资产方
        $this ->getAssets();
        //赋值调解员
        $this ->getMediator();
        SmartyAdapter::assign('arrList', $record);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('is_show', $is_show);
        SmartyAdapter::assign('page_query', http_build_query($query));
    }

    //导出
    public function exportAction()
    {

        $request = $this->getRequest();
        $mediatorId = $request->getQuery('mediatorId', '');
        $mobile = $request->getQuery('mobile', '');
        $caseId = $request->getQuery('caseId', '');
        $talkType = $request->getQuery('talkType', '');
        $litigantName = $request->getQuery('litigantName', '');

        $importRecordId = $request->getQuery('importRecordId', 0);
        $assetsId = $request->getQuery('assetsId', 0);
        $starttalkTime = $request->getQuery('starttalkTime', 0);
        $endtalkTime = $request->getQuery('endtalkTime', 0);


        $where = $this->getWhere();
        array_push($where,"callType = 1");

        if($mediatorId){//调解员
            array_push($where,"mediatorId = '{$mediatorId}' ");
        }
        if($mobile){//通话号码
            array_push($where,"callPhone = '{$mobile}'");
        }
        if($caseId){//按照案件号查询
            array_push($where,"caseId = '{$caseId}'");
        }
        if($talkType){//按照案件号查询
            array_push($where,"talkType = '{$talkType}'");
        }

        if($litigantName){
            array_push($where,"callName like '%{$litigantName}%'");
        }
        if($importRecordId){
             array_push($where,"importRecordId = '{$importRecordId}'");
        }
        if($assetsId){
            array_push($where,"assetsId = '{$assetsId}'");
        }
        if($starttalkTime){
            array_push($where,"talkTime >= '{$starttalkTime}'");
        }
        if($endtalkTime){
            array_push($where,"talkTime <= '{$endtalkTime}'");
        }

        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));

        $query['add_begin_end'] = $add_begin_end;
        $date_tmp =  explode(" 到 ",$add_begin_end);
        $begin_date = $date_tmp[0];
        $end_date = $date_tmp[1];
        if (!empty($begin_date) || !empty($end_date)) {
            $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
            $end_date = $end_date ? $end_date . " 23:59:59" : "";
        }
        if ($end_date) {
            $end_date = strtotime($end_date);
            array_push($where, "callTime <= {$end_date}");

        }
        if ($begin_date) {
            $begin_date = strtotime($begin_date);
            array_push($where, "callTime >= {$begin_date}");
        }
        $seacher = array(
            'where' => $where,
            'order' => 'id DESC'
        );
        $record = Module_Mediate_CallRecord::getInstance()->getAllBySearch(array("*"),$seacher,true);
        $assets = array_column(
            Module_InfoBase_Assets::getInstance()->getAllBySearch(
                ['id','name'],
                [],
                true
            ),
            'name',
            'id');
        //赋值调解部门
        $this ->getMediationOrgan();

        //赋值调解员
        $this ->getMediator();
        $data = [];

        if(!empty($record)){
            $this->getMediationOrgan();
            $mediationOrgan= SmartyAdapter::getTplVars('mediationOrgan');
            foreach($record as $k=>$v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['mediationOrganId'] = $mediationOrgan[$v['mediationOrganId']]['name'];
                $data[$k]['assets'] = $v['assetsId'] ? $assets[$v['assetsId']] : '';
                $data[$k]['caseId'] = $v['caseId'];
                $data[$k]['callName'] = $v['callName'];
                $data[$k]['callPhone'] = substr($v['callPhone'],0,3).'****'.substr($v['callPhone'],6,4);

                $data[$k]['mediatorName'] = $v['mediatorName'];
                $data[$k]['callTime'] = date('Y-m-d H:i:s',$v['callTime']);
                $data[$k]['callEndTime'] = date('Y-m-d H:i:s',$v['callEndTime']);
                $data[$k]['talkTime'] = $v['talkTime'];
                $data[$k]['talkType'] = $v['talkType']==1?'接通':'未接通';

            }
        }
        $header = ['序号', '调解部门','资产方', '案件号', '当事人', '手机号', '调解员', '呼叫时间','结束时间','通话时长','通话状态'];
        Lib_XlsxExport::getInstance()->downExcel($data,$header,'呼出记录');
        exit;
    }

}
